<?php
namespace app\pc\controller;

use think\Page;

class Lists extends Base{

    public $tid = 0;
    public $cid = 0;

    public function index(){

        $this->tid = intval(I('tid',0));
        $this->cid = intval(I('cid',0));

        $type_list = $this->getTypeList();

        $type_info = M('column')->where('status=1 and id='.$this->tid)->find();
        if((bool)$type_info && $type_info['type'] != 8 || $this->cid >0){

            $this->cid = ($this->cid == 0) ? $type_list[0]['id'] : $this->cid;
            $type_info = M('column')->where('status=1 and id='.$this->cid)->find();
        }

        $parent_type_name = M('column')->where('status=1 and id='.$this->tid)->getField('name');

        $this->assign('type_info',$type_info);
        $this->assign('column_name',$parent_type_name);
        $this->assign('type_list',$type_list);
        $this->assign('tid',$this->tid);
        $this->assign('cid',$this->cid);

        switch ($type_info['type']){
            case 1:return $this->news();//文章列表
            case 2:return $this->news();//文章列表
            case 3:return $this->article();//单篇文章
            case 4:return $this->photo();//图片
            case 7:return $this->twoNewsList();//三级栏目菜单
            case 8:return $this->twoIndexList();//二级引导页
            case 9:return $this->newsPic();//二级引导页
        }
    }

    /**栏目列表*/
    public function getTypeList(){
        $type_list = M('column')->where('status=1 and pid='.$this->tid)->order('id asc')->select();

        return $type_list;
    }

    /**二级引导页*/
    public function twoIndexList(){

        $column_list = M('column')->where('status=1 and pid ='.$this->tid)->order('id asc')->select();

        if($column_list){

            foreach ($column_list as $k=>$v){

                $list = M('article_news')
                    ->where('status=1 and pid ='.$v['id'])
                    ->order('id desc')
                    ->limit(8)
                    ->select();
                $column_list[$k]['list'] = $list;
            }
        }

        $this->assign('column_list',array_chunk($column_list,2));
        return $this->fetch('twoIndexList');
    }

    /**文章列表*/
    public function news(){

        $where = 'status=1 and pid='.$this->cid;


        $info_count = M('article_news')->where($where)->order('id desc')->count();

        $page = new Page($info_count,10);
        $newsList = M('article_news')
            ->where($where)
            ->order('id desc')
            ->limit($page->firstRow,$page->listRows)
            ->select();

        $this->assign('newsList',$newsList);
        $this->assign('page', $page->show());
        return $this->fetch('news');
    }

    /**图文列表*/
    public function newsPic(){

        $where = 'status=1 and pid='.$this->cid;
        $info = M('article_news')
            ->where($where)
            ->order('id desc')
            ->select();

        $this->assign('info', $info);
        return $this->fetch('newsPic');
    }

    /**二级文章列表*/
    public function twoNewsList(){

        $column = M('column')
            ->where('status=1 and pid='.$this->cid)
            ->order('id asc')
            ->select();

        if((bool)$column){

            foreach ($column as $k=>$v){

                $newsList = M('article_news')
                    ->where('status=1 and pid ='.$v['id'])
                    ->order('id desc')
                    ->select();
                $column[$k]['list'] = $newsList;
            }
        }

        $this->assign('tid',$this->tid);
        $this->assign('info',$column);
        return $this->fetch('twoNewsList');
    }

    /**栏目文章*/
    public function article(){

        $info = M('article_news')->where('status=1 and pid='.$this->cid)->order('id desc')->find();

        $this->assign('info',$info);
        return $this->fetch('article');
    }

    /**栏目图片*/
    public function photo(){

        $where = 'status=1 and pid='.$this->cid;

        $info_count = M('article_news')->where($where)->order('id desc')->count();

        $page = new Page($info_count,10);
        $info = M('article_news')
            ->where($where)
            ->order('id desc')
            ->limit($page->firstRow,$page->listRows)
            ->select();

        $this->assign('info', $info);
        $this->assign('page', $page->show());
        return $this->fetch('photo');
    }

    /**栏目三级文章*/
    public function activity(){
        $this->tid = intval(I('tid',0));
        $this->cid = intval(I('cid',0));

        $id = intval(I('id',0));

        $info =  M('article_news')->where('status=1 and id='.$id)->find();

        $type_list = $this->getTypeList();
        $type_info = M('column')->where('status=1 and id='.$this->cid)->find();
        $parent_type_name = M('column')->where('status=1 and id='.$this->tid)->getField('name');

        $this->assign('type_info',$type_info);
        $this->assign('column_name',$parent_type_name);
        $this->assign('type_list',$type_list);
        $this->assign('tid',$this->tid);
        $this->assign('cid',$this->cid);
        $this->assign('id',$id);

        $this->assign('info',$info);
        return $this->fetch('activity');
    }
}
